package com.bitejiuyeke.bitecommonmessage.service;


import com.aliyun.dypnsapi20170525.Client;
import com.aliyun.dypnsapi20170525.models.SendSmsVerifyCodeRequest;
import com.aliyun.dypnsapi20170525.models.SendSmsVerifyCodeResponse;
import com.aliyun.dypnsapi20170525.models.SendSmsVerifyCodeResponseBody;
import com.bitejiuyeke.bitecommoncore.utils.JsonUtil;
import com.bitejiuyeke.bitecommondomain.constants.MessageConstants;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 短信服务
 */
@Component
@Slf4j
public class AliSmsService {
    /**
     * 客户端
     */
    @Autowired
    private Client client;

    /**
     * 短信模板代码
     */
    @Value("${sms.aliyun.templateCode}")
    private String templateCode;

    /**
     * 签名
     */
    @Value("${sms.sing-name}")
    private String singName;

    //是否发送验证码
    @Value("${sms.send-message:false}")
    private boolean sendMessage;

    //过期时间，单位为分钟
    @Value("${sms.code-expiration:5}")
    private long deadline;

    /**
     * 发送短信验证码
     * @param phone 手机号
     * @param code 验证码
     * @return 是否发送成功
     */
    public boolean sendMobileCode(String phone,String code) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("code", code);
        params.put("min", deadline+"");
        return sendTemMessage(phone,templateCode,params);
    }



    /**
     * 发送模板短信
     * @param phone 手机号
     * @param templateCode 模板编码
     * @param params 参数
     * @return 是否发送成功
     */
    private boolean sendTemMessage(String phone, String templateCode, Map<String,String> params){
        //是否发送线上短信由nacos决定
        if(!sendMessage){
            log.error("短信发送通道关闭,{}",phone);
            return false;
        }
        //发送短信
        SendSmsVerifyCodeRequest request = new SendSmsVerifyCodeRequest();
        request.setSignName(singName);
        request.setTemplateCode(templateCode);
        request.setPhoneNumber(phone);
        request.setTemplateParam(JsonUtil.obj2String(params));
        request.setValidTime(deadline*60L);//设置有效期，单位为秒
        //3 发送请求根据结果是否成功
        try {

            SendSmsVerifyCodeResponse sendSmsResponse =client.sendSmsVerifyCode(request);
            //SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
          // SendSmsResponseBody responseBody= sendSmsResponse.getBody();
            SendSmsVerifyCodeResponseBody responseBody = sendSmsResponse.getBody();
            if (responseBody.getCode().equals(MessageConstants.SMS_MSG_OK)){
                return true;
            }
            log.error("短信{}发送失败，失败原因{}",new Gson().toJson(request),responseBody.getMessage());
            return false;
        } catch (Exception e) {
            log.error("短信{}发送失败，失败原因{}。。。",new Gson().toJson(request),e.getMessage());
            return false;
        }

    }

}
